package jupiter.servlets;

import java.io.IOException;
import java.net.URLEncoder;
import org.mortbay.jetty.RetryRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


//import jupiter.messaging.Inbox;
import jupiter.message.Message;
import jupiter.util.*;

import org.json.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jupiter.channel.Session;
import jupiter.filters.*;

public class JSONServlet extends HttpServlet {
	Logger logger = LoggerFactory.getLogger(JSONServlet.class);
	public void service(HttpServletRequest req, HttpServletResponse res) throws java.io.IOException {
		int loops = java.lang.Integer.parseInt(req.getParameter("loops"));
		boolean orgjson = java.lang.Boolean.parseBoolean(req.getParameter("orgjson"));
		if(orgjson){
			JSONObject json = new JSONObject();
			JSONObject inner = new JSONObject();
			JSONObject data = new JSONObject();
			try {
				inner.put("to", "file/@bignode.jabify.com");
				inner.put("action", "create");
				inner.put("type", "channel");
				inner.put("from", "something");
				data.put("subscriptions", new JSONArray());
				data.put("keepList", true);
				data.put("address", "file/cakefightsmelly35@bignode.jabify.com");
				data.put("name", "cakefightsmelly35");
				data.put("publicInfo", new JSONObject());
				data.put("secret", "eae78b16-50a0-4221-a803-fd2a26d23dc8");
				inner.put("data", data);
				json.put("channel", inner);
				json.put("createdAt", System.currentTimeMillis());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String test;
			Timey t = new Timey();
			int i;
			for(i=0; i<loops; i++){
				test=json.toString();
			}
			t.end("toString");
		} else {
			JSONObject json = new JSONObject();
			JSONObject inner = new JSONObject();
			JSONObject data = new JSONObject();
			try {
				inner.put("to", "file/@bignode.jabify.com");
				inner.put("action", "create");
				inner.put("type", "channel");
				inner.put("from", "something");
				data.put("subscriptions", new JSONArray());
				data.put("keepList", true);
				data.put("address", "file/cakefightsmelly35@bignode.jabify.com");
				data.put("name", "cakefightsmelly35");
				data.put("publicInfo", new JSONObject());
				data.put("secret", "eae78b16-50a0-4221-a803-fd2a26d23dc8");
				inner.put("data", data);
				json.put("channel", inner);
				json.put("createdAt", System.currentTimeMillis());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String test;
			Timey t = new Timey();
			int i;
			for(i=0; i<loops; i++){
				test=json.toString();
			}
			t.end("toString");
		}
		RequestHandler.sendResult(req, res, "took seconds");
	}
	
}
